<?php
/**
 * Created by PhpStorm.
 * User: 睿
 * Date: 2017-11-13
 * Time: 11:52
 */

namespace app\store\controller;
use service\DataService;
use think\Db;


use controller\BasicAdmin;

class Mreduce extends BasicAdmin
{
    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'StoreMreduce';

    /**
     * 订货单列表
     * @return array
     */
    public function index()
    {
        $this->title = '待出库原料列表';
        $get = $this->request->get();

        $store_id = session('user')['store_id'];

        $join = [
            ['ys_goods_material g', 'a.material_id=g.id', 'LEFT'],
            ['ys_goods_material_category c', 'g.cat_id=c.id', 'LEFT'],
        ];

        $db = Db::name($this->table)->alias('a')->where('a.store_id', $store_id)->where('a.is_deleted', 0)->where('a.status', 0)->where('type',0)->field('g.name,c.name as cname,a.id,a.addtime,g.unit,a.num')->join($join);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        return parent::_list($db);
    }

    /**
     * 添加原材料
     * @return array|string
     */
    public function add()
    {
        $store_id = session('user')['store_id'];
        if (request()->ispost()) {
            $insert = db($this->table)->insert(['material_id' => input('material_id'), 'num' => input('num'), 'store_id' => $store_id, 'addtime' => time()]);
            if ($insert !== false)
                $this->success('原料添加成功！', '');
        } else {
            $db = db('store_material_stock')->alias('a')->join('goods_material b','a.material_id=b.id','LEFT')->select();
            $this->assign('list', $db);

            return $this->_form($this->table, 'add');
        }
    }

    /**
     * 编辑原材料
     * @return array|string
     */
    public function edit()
    {
        $db = db('store_material_stock')->alias('a')->join('goods_material b','a.material_id=b.id','LEFT')->select();

        $this->assign('list', $db);

        return $this->_form($this->table, 'add');
    }


    /**
     * 删除原材料
     */
    public function del()
    {
        if (DataService::update($this->table)) {
            $this->success("原材料删除成功！", '');
        }
        $this->error("原材料删除失败，请稍候再试！");
    }

}